package o;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.huawei.appgallery.foundation.ui.framework.widget.dialog.dialogactivity.DialogActivity.a;
import com.huawei.fastengine.fastview.NavigationUtils;
import com.huawei.hwid.core.datatype.UserInfo;
import java.util.ArrayList;
import java.util.List;
import o.ewm.h;
import o.ewm.j;
import o.ewm.n;

public class dya implements dxy {
    private dxz ˊ;

    public dya(dxz o_dxz) {
        this.ˊ = o_dxz;
    }

    public void ˋ() {
        List arrayList = new ArrayList();
        List arrayList2 = new ArrayList();
        ˏ(arrayList, arrayList2);
        if (arrayList2.size() > 1) {
            final a aVar = new a(this.ˊ.ॱॱ(), "EmailSelectDialog");
            LayoutInflater from = LayoutInflater.from(this.ˊ.ॱॱ());
            View inflate = from.inflate(h.ʽʼ, null);
            LinearLayout linearLayout = (LinearLayout) inflate.findViewById(j.ʽߴ);
            ˎ(arrayList2, aVar, from, linearLayout);
            View inflate2 = from.inflate(h.ʼʻ, null);
            ((TextView) inflate2.findViewById(j.ᐝʹ)).setText(n.ՙ);
            linearLayout.addView(inflate2);
            inflate2.setOnClickListener(new OnClickListener(this) {
                final /* synthetic */ dya ˋ;

                public void onClick(View view) {
                    this.ˋ.ॱ("");
                    aVar.ˊ();
                }
            });
            aVar.ˊ(n.ˊˉ).ˎ(inflate, null).ˊ(-1, 8).ˊ(-2, 8).ˎ();
        } else if (arrayList2.size() == 1) {
            ॱ((String) arrayList2.get(0));
        } else if (arrayList2.size() != 0 || arrayList.size() <= 0) {
            evk.ˏ(ContentManager.getInstant().getContext(), n.ͺᐧ, 0).ˋ();
        } else {
            ˏ();
        }
    }

    private void ˎ(List<String> list, final a aVar, LayoutInflater layoutInflater, LinearLayout linearLayout) {
        PackageManager packageManager = this.ˊ.ॱॱ().getPackageManager();
        for (int i = 0; i < list.size(); i++) {
            final String str = (String) list.get(i);
            try {
                PackageInfo packageInfo = packageManager.getPackageInfo(str, 0);
                View inflate = layoutInflater.inflate(h.ʼʼ, null);
                ImageView imageView = (ImageView) inflate.findViewById(j.יˋ);
                ((TextView) inflate.findViewById(j.ـͺ)).setText(packageInfo.applicationInfo.loadLabel(packageManager));
                imageView.setImageDrawable(packageInfo.applicationInfo.loadIcon(packageManager));
                inflate.setOnClickListener(new OnClickListener(this) {
                    final /* synthetic */ dya ˋ;

                    public void onClick(View view) {
                        this.ˋ.ॱ(str);
                        aVar.ˊ();
                    }
                });
                if (i != 0) {
                    linearLayout.addView(layoutInflater.inflate(h.ʽʻ, null));
                }
                linearLayout.addView(inflate);
            } catch (NameNotFoundException e) {
                Logger.e("DefaultEmailAdapter", "can not find pkg:" + str);
            }
        }
    }

    private void ˏ(List<String> list, List<String> list2) {
        Intent intent = new Intent("android.intent.action.SENDTO", Uri.parse(NavigationUtils.MAIL_SCHEMA_PREF));
        intent.putExtra("android.intent.extra.EMAIL", new String[]{UserInfo.ADDRESS});
        Intent intent2 = new Intent("android.intent.action.SEND");
        intent2.putExtra("android.intent.extra.SUBJECT", this.ˊ.ˊ());
        intent2.putExtra("android.intent.extra.TEXT", this.ˊ.ˏ());
        if (this.ˊ.ˋ() != null) {
            intent2.putExtra("android.intent.extra.STREAM", this.ˊ.ˋ());
            intent2.addFlags(1);
            intent2.setType(this.ˊ.ॱ());
        }
        List<ResolveInfo> queryIntentActivities = this.ˊ.ॱॱ().getPackageManager().queryIntentActivities(intent, 0);
        List<ResolveInfo> queryIntentActivities2 = this.ˊ.ॱॱ().getPackageManager().queryIntentActivities(intent2, 0);
        if (queryIntentActivities != null && queryIntentActivities.size() > 0) {
            for (ResolveInfo resolveInfo : queryIntentActivities) {
                if (!(resolveInfo == null || resolveInfo.activityInfo == null || TextUtils.isEmpty(resolveInfo.activityInfo.packageName))) {
                    list.add(resolveInfo.activityInfo.packageName);
                }
            }
        }
        if (queryIntentActivities2 != null && queryIntentActivities2.size() > 0) {
            for (ResolveInfo resolveInfo2 : queryIntentActivities2) {
                if (!(resolveInfo2 == null || resolveInfo2.activityInfo == null || TextUtils.isEmpty(resolveInfo2.activityInfo.packageName) || !list.contains(resolveInfo2.activityInfo.packageName))) {
                    list2.add(resolveInfo2.activityInfo.packageName);
                }
            }
        }
    }

    private void ˏ() {
        Intent intent = new Intent("android.intent.action.SENDTO");
        String[] strArr = (String[]) this.ˊ.ˎ().toArray(new String[0]);
        intent.setData(Uri.parse(NavigationUtils.MAIL_SCHEMA_PREF));
        intent.putExtra("android.intent.extra.EMAIL", strArr);
        intent.putExtra("android.intent.extra.SUBJECT", this.ˊ.ˊ());
        intent.putExtra("android.intent.extra.TEXT", this.ˊ.ˏ());
        if (!(this.ˊ.ॱॱ() instanceof Activity)) {
            intent.setFlags(268435456);
        }
        try {
            this.ˊ.ॱॱ().startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Logger.e("DefaultEmailAdapter", "can not start send email activity");
        }
    }

    private void ॱ(String str) {
        String[] strArr = (String[]) this.ˊ.ˎ().toArray(new String[0]);
        Intent intent = new Intent("android.intent.action.SEND");
        intent.putExtra("android.intent.extra.EMAIL", strArr);
        intent.putExtra("android.intent.extra.SUBJECT", this.ˊ.ˊ());
        intent.putExtra("android.intent.extra.TEXT", this.ˊ.ˏ());
        if (this.ˊ.ˋ() != null) {
            intent.putExtra("android.intent.extra.STREAM", this.ˊ.ˋ());
            intent.addFlags(1);
            intent.setType(this.ˊ.ॱ());
        }
        if (!(this.ˊ.ॱॱ() instanceof Activity)) {
            intent.setFlags(268435456);
        }
        if (!TextUtils.isEmpty(str)) {
            intent.setPackage(str);
        }
        try {
            this.ˊ.ॱॱ().startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Logger.e("DefaultEmailAdapter", "can not start activity:" + str);
        }
    }
}
